package com.liang.controller;


import com.liang.common.components.JwtTokenUtil;
import com.liang.common.lang.Result;
import com.liang.entity.CUser;
import com.liang.entity.CUserDetail;
import com.liang.service.CUserDetailService;
import com.liang.service.CUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李昂
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class CUserController {

    private CUserService userService;
    private JwtTokenUtil jwtTokenUtil;
    private CUserDetailService userDetailService;


    /**
     * @Author 李昂
     * @Description 用户登录
     * @Date 2020/12/3 14:21
     */
    @PostMapping("/login")
    public Result login(@RequestParam("username") String username, @RequestParam("password") String password) {
        CUser user = null;
        try {
            user = userService.checklogin(username, password);
            return Result.succ(jwtTokenUtil.generateToken(user.getSno()));
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * @Author 李昂
     * @Description 登陆成功后 用户拿着服务器给用户的token值向服务器要自己的身份信息
     * @Date 2020/12/7 14:40
     */
    @GetMapping("/mine")
    public Result myMsg() {
        //从上下文中获取用户信息,  注意千万不要把你的token值暴露出去不然你的信息将会全部暴露
        UserDetails userDetails = (UserDetails) org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        CUser user = userService.getUserBySno(userDetails.getUsername());
        if (user == null) {
            return Result.fail("用户名密码错误");
        } else {
            return Result.succ(user);
        }
    }


    /**
     * @Author 李昂
     * @Description 注册
     * @Date 2020/12/2 16:54
     */
    @PostMapping("/regist")
    public Result regist(@RequestBody @Validated CUser user) {
        try {
            userService.register(user);
            return Result.succ();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }


    /**
     * @Author 李昂
     * @Description 获取全部用户
     * @Date 2020/12/3 16:48
     */
    @GetMapping("/allUser")
    public Result users() {
        return Result.succ(userService.allUsers());
    }


    /**
     * @Author 李昂
     * @Description 补充用户信息用户信息
     * @Date 2020/12/9 16:20
     */
    @PutMapping
    public Result updateUserMsg(@RequestParam("id") String id, @RequestBody CUserDetail userDetail) {
        if (userDetailService.updateUserMsg(id, userDetail)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    /**
     * @Author 李昂
     * @Description 修改个人信息
     * @Date 2020/12/10 14:33
     */
    @PutMapping("/allmsg")
    public Result updateAllMsg(@RequestParam("id") String id, @RequestBody CUser user) {
        if (userService.updateAllMsg(id, user)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //获取优秀社员的信息
    @GetMapping("/exce")
    public Result getExcellentUser() {
        return Result.succ(userService.getExcellentUser());
    }


    //用户修改密码
    @PutMapping("/pwd")
    public Result modifypwd(@RequestParam("oldpwd") String oldpwd, @RequestParam("newpwd") String newpwd, @RequestParam("id") String id, @RequestParam("encodepwd") String encodepwd) {
        if (!new BCryptPasswordEncoder().matches(oldpwd, encodepwd)) {
            return Result.fail("原始密码错误");
        }
        if (userService.modifypwd(id, newpwd)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }

    //加载后台桌面数据
    @GetMapping("/notice")
    public Result getNotice() {
        return Result.succ(userService.getNotice());
    }


}
